﻿using System.IO;
using System.IO.Compression;
using System.Net.Mail;


namespace IphoneTrackerDotNet
{
    public class Util
    {

        public static void SendGMail(string de, string para, string assunto, string pathAttach, string user, string pass)
        {
            var loginInfo = new System.Net.NetworkCredential(user, pass);
            var msg = new MailMessage {From = new MailAddress(de)};
            msg.To.Add(new MailAddress(para));
            msg.Subject = "Iphone Track - " + assunto;
            msg.Body = "Vide Anexo";
            msg.IsBodyHtml = true;

            var attachment = new Attachment(pathAttach);
            msg.Attachments.Add(attachment);


            var client = new SmtpClient("smtp.gmail.com")
                             {
                                 EnableSsl = true,
                                 UseDefaultCredentials = false,
                                 Credentials = loginInfo
                             };
            client.Send(msg);
        }

        public static void Compress(FileInfo fi, string outFilegz)
        {
            // Get the stream of the source file.
            using (FileStream inFile = fi.OpenRead())
            {
                // Prevent compressing hidden and already compressed files.
                if ((File.GetAttributes(fi.FullName) & FileAttributes.Hidden)
                        != FileAttributes.Hidden & fi.Extension != ".gz")
                {
                    // Create the compressed file.
                    using (FileStream outFile = File.Create(outFilegz))
                    {
                        using (var compress = new GZipStream(outFile, CompressionMode.Compress))
                        {
                            // Copy the source file into the compression stream.
                            var buffer = new byte[4096];
                            int numRead;
                            while ((numRead = inFile.Read(buffer, 0, buffer.Length)) != 0)
                            {
                                compress.Write(buffer, 0, numRead);
                            }

                        }
                    }
                }
            }
        }




    }

}
